<?php
include_once('inc/functions.inc.php');

if (!isEmpty(getParam('operation'))) {

    switch (getParam('operation')) {
        case 'filtro': {
                fntAcademicoAsignacionEstudiantesFiltro($db);
                break;
            }
        case 'contenido': {
                fntAcademicoAsignacionEstudiantesContenido($db);
                break;
            }
        case 'secciones': {
                fntAcademicoAsignacionEstudiantesContenidoSecciones($db);
                break;
            }
        case 'cargos': {
                fntAcademicoAsignacionEstudiantesContenidoCargos($db);
                break;
            }
        case 'reporte': {
                fntAcademicoAsignacionEstudiantesReporte($db);
                break;
            }
        case 's': {
                fntAcademicoAsignacionEstudiantesGrabar($db);
                break;
            }
    }
    die();
}

function fntAcademicoAsignacionEstudiantesFiltro($db) {
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES
    ?>
    <script type="text/javascript">
        var intFilaPadres = 0;
    </script>
    <br>
    <br>

    <div style="width: 100px; height: 30px; float: left;"><b>Estudiante</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE" id="txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE" style="width: 200px;">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_student WHERE STATUS = '1'";
            $qTMP = $db->queryToArray($strQuery, true);
            foreach ($qTMP as $rTMP) {
                $strSelected = ( $strEstudiante == $rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php echo $rTMP["ID"]; ?>" <?php echo $strSelected; ?>><?php echo $rTMP["FIRST_NAME"] . " " . $rTMP["LAST_NAME"]; ?></option>
                <?php
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <style>
        .ui-button { margin-left: -1px; }
        .ui-button-icon-only .ui-button-text { padding: 0.01em; } 
        .ui-autocomplete-input { margin: 0; padding: 0.0em 0 0.0em 0.0em; }
    </style>
    <script language="javascript" type="text/javascript">
        (function( $ ) {
            $.widget( "ui.combobox", {
                _create: function() {
                    var self = this,
                    select = this.element.hide(),
                    selected = select.children( ":selected" ),
                    value = selected.val() ? selected.text() : "";
                    var input = this.input = $( "<input>" )
                    .insertAfter( select )
                    .val( value )
                    .css("width", 300)
                    .autocomplete({
                        delay: 0,
                        minLength: 0,
                        source: function( request, response ) {
                            var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
                            response( select.children( "option" ).map(function() {
                                var text = $( this ).text();
                                if ( this.value && ( !request.term || matcher.test(text) ) )
                                    return {
                                        label: text.replace(
                                        new RegExp(
                                        "(?![^&;]+;)(?!<[^<>]*)(" +
                                            $.ui.autocomplete.escapeRegex(request.term) +
                                            ")(?![^<>]*>)(?![^&;]+;)", "gi"
                                    ), "<strong>$1</strong>" ),
                                        value: text,
                                        option: this
                                    };
                            }) );
                        },
                        select: function( event, ui ) {  
                            ui.item.option.selected = true;
                            self._trigger( "selected", event, {
                                item: ui.item.option
                            });
            							
                            $("[name='txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE']").val(ui.item.option.value);
                            //alert($("#"+$strAcademicoAsignacionEstudiantesForm).serialize());
                            $("[name='hdnAcademicoAsignacionEstudiantes_Id']").val("");
                            fntAcademicoAsignacionEstudiantesContenido();
                            fntAcademicoAsignacionEstudiantesReporte();
                        },
                        change: function( event, ui ) {
                            if ( !ui.item ) {
                                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
                                valid = false;
                                select.children( "option" ).each(function() {
                                    if ( $( this ).text().match( matcher ) ) {
                                        this.selected = valid = true;
                                        return false;
                                    }
                                });
                                if ( !valid ) {
                                    // remove invalid value, as it didn't match anything
                                    $( this ).val( "" );
                                    select.val( "" );
                                    input.data( "autocomplete" ).term = "";
                                    return false;
                                }
                            }
                        }
                    })
                    .addClass( "ui-widget ui-widget-content ui-corner-left" );

                    input.data( "autocomplete" )._renderItem = function( ul, item ) {
                        return $( "<li></li>" )
                        .data( "item.autocomplete", item )
                        .append( "<a>" + item.label + "</a>" )
                        .appendTo( ul );
                    };

                    this.button = $( "<button type='button'>&nbsp;</button>" )
                    .attr( "tabIndex", -1 )
                    .attr( "title", "Show All Items" )
                    .insertAfter( input )
                    .button({
                        icons: {
                            primary: "ui-icon-triangle-1-s"
                        },
                        text: false
                    })
                    .removeClass( "ui-corner-all" )
                    .addClass( "ui-corner-right ui-button-icon" )
                    .click(function() {
                        // close if already visible
                        if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                            input.autocomplete( "close" );
                            return;
                        }

                        // work around a bug (likely same cause as #5265)
                        $( this ).blur();

                        // pass empty string as value to search for, displaying all results
                        input.autocomplete( "search", "" );
                        input.focus();
                    });
                },

                destroy: function() {
                    this.input.remove();
                    this.button.remove();
                    this.element.show();
                    $.Widget.prototype.destroy.call( this );
                }
            });
        })( jQuery );

        $(function() {
            $("#txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE").combobox();
        });    
    </script> 
    <script type="text/javascript">
        $(function() {
            $("[name='txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE']").change( function() {
            			
                //$("[name='hdnAcademicoAsignacionEstudiantes_Id']").val("");
                fntAcademicoAsignacionEstudiantesContenido();
                fntAcademicoAsignacionEstudiantesReporte();
            });
        });
    </script>
    <?php
}

function fntAcademicoAsignacionEstudiantesContenido($db) {
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES

    $strId = isset($_POST["hdnAcademicoAsignacionEstudiantes_Id"]) ? $_POST["hdnAcademicoAsignacionEstudiantes_Id"] : "";
    $strEstudiante = isset($_POST["txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE"]) ?
            $_POST["txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE"] : "";
    if (!empty($strId)) {
        $strQuery = "SELECT  FK_ESTUDIANTE, FK_PERIODO, FK_GRADO, FK_SECCION, DATE_FORMAT(FECHA_ASIGNACION, '%d-%m-%Y') AS FECHA_ASIGNACION   
                    FROM    cllg_academico_estudiantes_asignacion WHERE ID = '{$strId}'";
    }
    if (!empty($strEstudiante)) {
        $strQuery = "SELECT  *   FROM    cllg_student WHERE ID = '{$strEstudiante}'";
    }

    $qTMP = $db->query($strQuery);
    while ($rTMP = $db->fetch_array($qTMP)) {
        $arrInfo = $rTMP;
    }
    $db->free_result($qTMP);
    $strEstudiante = $strPeriodo = $strGrado = $strSeccion = $strFechaAsignacion=null;
    if (!empty($strId)) {
        $strEstudiante = isset($arrInfo["FK_ESTUDIANTE"]) ? $arrInfo["FK_ESTUDIANTE"] : "";
        $strPeriodo = isset($arrInfo["FK_PERIODO"]) ? $arrInfo["FK_PERIODO"] : "";
        $strGrado = isset($arrInfo["FK_GRADO"]) ? $arrInfo["FK_GRADO"] : "";
        $strSeccion = isset($arrInfo["FK_SECCION"]) ? $arrInfo["FK_SECCION"] : "";
        $strFechaAsignacion = isset($arrInfo["FECHA_ASIGNACION"]) ? $arrInfo["FECHA_ASIGNACION"] : "";
    }
    ?>
    <script type="text/javascript">
        var intFilaPadres = 0;
    </script>
    <br>
    <br>
    <div id="divContenidoEstudianteDetalle" style="width: 1000px; float: left;">
        <?php
        $arrInfo = array();
        $strQuery = "SELECT  P.*  
                    FROM    cllg_student AS E,
                            cllg_parent AS P,
                            cllg_student_parent AS EP
                    WHERE   E.ID = EP.STUDENT
                    AND     P.ID = EP.PARENT
                    AND     E.ID = '{$strEstudiante}'";
        $qTMP = $db->queryToArray($strQuery, true);
        foreach ($qTMP as $rTMP) {
            $arrInfo = $rTMP;
        }
        if (count($arrInfo) == 0) {
            $arrInfo = array("NOMBRE_A" => null, "APELLIDO_A" => null, "RAZON_SOCIAL" => null);
        }
        ?>
        <div style="width: 100px; float: left;"><b>Padre</b></div>
        <div style="width: 900px; float: left;">&nbsp;<?php echo $arrInfo["NOMBRE_A"] . " " . $arrInfo["APELLIDO_A"]; ?></div>    
        <?php
        $arrInfo = array();
        $strQuery = "SELECT  B.*  
                    FROM    cllg_student AS E,
                            cllg_benefactor AS B,
                            cllg_student_benefactor AS EB
                    WHERE   E.ID = EB.STUDENT
                    AND     B.ID = EB.BENEFACTOR
                    AND     E.ID = '{$strEstudiante}'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $arrInfo = $rTMP;
        }
        if (count($arrInfo) == 0) {
            $arrInfo = array("RAZON_SOCIAL" => null);
        }
        $db->free_result($qTMP);
        ?>
        <div style="width: 100px; float: left;"><b>Benefactor</b></div>
        <div style="width: 900px; float: left;">&nbsp;<?php echo $arrInfo["RAZON_SOCIAL"]; ?></div>    
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Periodo</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionEstudiantes_fk-periodo" style="width: 200px;">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_periodos WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ($rTMP = $db->fetch_array($qTMP)) {
                $strSelected = ( $strPeriodo == $rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php echo $rTMP["ID"]; ?>" <?php echo $strSelected; ?>><?php echo $rTMP["NOMBRE"]; ?></option>
                <?php
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Grado</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <select name="txtAcademicoAsignacionEstudiantes_fk-grado" style="width: 200px;">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_grados WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ($rTMP = $db->fetch_array($qTMP)) {
                $strSelected = ( $strGrado == $rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php echo $rTMP["ID"]; ?>" <?php echo $strSelected; ?>><?php echo $rTMP["CODIGO"]; ?> - <?php echo $rTMP["NOMBRE"]; ?></option>
                <?php
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Sección</b></div>
    <div style="width: 900px; height: 30px; float: left;" id="divAcademicoAsignacionEstudiantesContenidoSeccion">
        <select name="txtAcademicoAsignacionEstudiantes_fk-seccion">
            <option value="0"></option>
            <?php
            $strQuery = "SELECT  * 
                        FROM    cllg_academico_secciones,
                                cllg_academico_grados_secciones 
                        WHERE   cllg_academico_secciones.ID = cllg_academico_grados_secciones.FK_SECCION
                        AND     cllg_academico_grados_secciones.FK_GRADO = '{$strGrado}'";
            $qTMP = $db->query($strQuery);
            while ($rTMP = $db->fetch_array($qTMP)) {
                $strSelected = ( $strSeccion == $rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php echo $rTMP["ID"]; ?>" <?php echo $strSelected; ?> ><?php echo $rTMP["NOMBRE"]; ?></option>
                <?php
            }
            $db->free_result($qTMP);
            ?>
        </select>    
    </div>
    <div style="width: 100px; height: 30px; float: left;"><b>Fecha Inscripcion</b></div>
    <div style="width: 900px; height: 30px; float: left;">
        <input type="text" name="txtAcademicoAsignacionEstudiantes_FECHA-ASIGNACION" value="<?php echo $strFechaAsignacion; ?>" style="text-align: center;" maxlength="10">
    </div>
    <div style="width: 1000px; float: left;" id="divContenidoEstudianteCargos">
        <table width="50%" cellpadding="2" cellspacing="2" border="0">
            <tr>
                <td colspan="4" align="center" style="background-color: #ECEAE0;"><b>Cargos</b></td>
            </tr>
            <tr>
                <td><b>Descripcion</b></td>
                <td align="center"><b>Valor</b></td>
                <td align="center"><b>Vigencia</b></td>
                <td align="center"><b>Tipo</b></td>
            </tr>
            <?php
            $strQuery = "SELECT  * 
                    FROM    cllg_academico_cargos,
                            cllg_academico_cargos_detail 
                    WHERE   cllg_academico_cargos.ID = cllg_academico_cargos_detail.FK_CARGOS
                    AND     cllg_academico_cargos.FK_PERIODO = '{$strPeriodo}'
                    AND     cllg_academico_cargos.FK_GRADO = '{$strGrado}'";
            //AND     cllg_academico_cargos_detail.TIPO = 'padres'";
            $qTMP = $db->query($strQuery);
            while ($rTMP = $db->fetch_array($qTMP)) {
                ?>
                <tr>
                    <td><?php echo $rTMP["DESCRIPCION"]; ?></td>
                    <td align="right"><?php echo $rTMP["VALOR"]; ?>&nbsp;</td>
                    <td align="center"><?php echo $rTMP["VIGENCIA"]; ?></td>
                    <td align="center"><?php echo strtoupper($rTMP["TIPO"]); ?></td>
                </tr>
                <?php
            }
            $db->free_result($qTMP);
            ?>
        </table>    
    </div>
    <div style="width: 500px; height: 30px; float: left; border-bottom: 1px solid black">&nbsp;</div>
    <div style="width: 500px; height: 30px; float: left;">&nbsp;</div>
    <script type="text/javascript">
        $(function(){
            $("input[name='txtAcademicoAsignacionEstudiantes_FECHA-ASIGNACION']").datepicker({
                changeMonth: true,
                changeYear: true,
                monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
                dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
                dateFormat: 'dd-mm-yy'
            });
            $("#btnAcademicoPensumAgregar").click( function () {
                fntAcademicoPensumMateriaAgregar();
            });
            $("[name='txtAcademicoAsignacionEstudiantes_fk-periodo']").change( function () {
                fntAcademicoAsignacionEstudiantesContenidoCargos();
            });
            $("[name='txtAcademicoAsignacionEstudiantes_fk-grado']").change( function () {
                fntAcademicoAsignacionEstudiantesContenidoSeccion();
                fntAcademicoAsignacionEstudiantesContenidoCargos();
            });     
        });
    </script>
    <?php
}

function fntAcademicoAsignacionEstudiantesContenidoSecciones($db) {

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    $strGrado = isset($_POST["txtAcademicoAsignacionEstudiantes_fk-grado"]) ? $_POST["txtAcademicoAsignacionEstudiantes_fk-grado"] : "";
    ?>
    <select name="txtAcademicoAsignacionEstudiantes_fk-seccion">
        <option value="0"></option>
        <?php
        $strQuery = "SELECT  * 
                FROM    cllg_academico_secciones,
                        cllg_academico_grados_secciones 
                WHERE   cllg_academico_secciones.ID = cllg_academico_grados_secciones.FK_SECCION
                AND     cllg_academico_grados_secciones.FK_GRADO = '{$strGrado}'";
        $qTMP = $db->query($strQuery);
        while ($rTMP = $db->fetch_array($qTMP)) {
            $strSelected = ( $strGrado == $rTMP["ID"] ) ? "selected" : "";
            ?>
            <option value="<?php echo $rTMP["ID"]; ?>"><?php echo $rTMP["NOMBRE"]; ?></option>
            <?php
        }
        $db->free_result($qTMP);
        ?>
    </select>
    <?php
}

function fntAcademicoAsignacionEstudiantesContenidoCargos($db) {

    $strPeriodo = isset($_POST["txtAcademicoAsignacionEstudiantes_fk-periodo"]) ? $_POST["txtAcademicoAsignacionEstudiantes_fk-periodo"] : "";
    $strGrado = isset($_POST["txtAcademicoAsignacionEstudiantes_fk-grado"]) ? $_POST["txtAcademicoAsignacionEstudiantes_fk-grado"] : "";
    ?>
    <table width="50%" cellpadding="2" cellspacing="2" border="0">
        <tr>
            <td colspan="4" align="center" style="background-color: #ECEAE0;"><b>Cargos</b></td>
        </tr>
        <tr>
            <td><b>Descripcion</b></td>
            <td align="center"><b>Valor</b></td>
            <td align="center"><b>Vigencia</b></td>
            <td align="center"><b>Tipo</b></td>
        </tr>
        <?php
        $strQuery = "SELECT  * 
                FROM    cllg_academico_cargos,
                        cllg_academico_cargos_detail 
                WHERE   cllg_academico_cargos.ID = cllg_academico_cargos_detail.FK_CARGOS
                AND     cllg_academico_cargos.FK_PERIODO = '{$strPeriodo}'
                AND     cllg_academico_cargos.FK_GRADO = '{$strGrado}'";
        //AND     cllg_academico_cargos_detail.TIPO = 'padres'";
        $qTMP = $db->queryToArray($strQuery, true);
        foreach ($qTMP as $rTMP) {
            ?>
            <tr>
                <td><?php echo $rTMP["DESCRIPCION"]; ?></td>
                <td align="right"><?php echo $rTMP["VALOR"]; ?>&nbsp;</td>
                <td align="center"><?php echo $rTMP["VIGENCIA"]; ?></td>
                <td align="center"><?php echo strtoupper($rTMP["TIPO"]); ?></td>
            </tr>
            <?php
        }
        ?>
    </table>    
    <?php
}

function fntAcademicoAsignacionEstudiantesContenidoPeriodoGradoDetalle($db) {
    
}

function fntAcademicoAsignacionEstudiantesReporte($db) {
    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    $strEstudiante = isset($_POST["txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE"]) ? $_POST["txtAcademicoAsignacionEstudiantes_FK-ESTUDIANTE"] : "";
    $strQuery = "SELECT CA.ID, CA.FK_ESTUDIANTE, CA.FK_PERIODO, CA.FK_GRADO, 
                        DATE_FORMAT(CA.FECHA_ASIGNACION, '%d-%m-%Y') AS FECHA_ASIGNACION, 
                        E.FIRST_NAME, E.LAST_NAME,  
                        P.NOMBRE AS PERIODO,
                        G.NOMBRE AS GRADO,
                        S.NOMBRE AS SECCION 
                FROM    cllg_academico_estudiantes_asignacion AS CA,
                        cllg_student AS E,
                        cllg_academico_periodos AS P,
                        cllg_academico_grados AS G,
                        cllg_academico_secciones AS S 
                WHERE   CA.FK_ESTUDIANTE = E.ID
                AND     CA.FK_PERIODO = P.ID
                AND     CA.FK_GRADO = G.ID
                AND     CA.FK_SECCION = S.ID
                AND     CA.FK_ESTUDIANTE = '{$strEstudiante}'
                ORDER   BY E.FIRST_NAME, E.LAST_NAME";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    ?>
    <style type="">
        table {
            border-collapse: collapse;
        }
        table td{
            border: 1px solid #AAAAAA;        
        }
        .oddtr {
            background-color:#FFFFFF;
        }
        .eventr {
            background-color:#FFFFFF;
        }
        .trover {
            background-color: #ECEAE0;
        }
        .trclick {
            background-color: #ECEAE0;
        }
    </style>
    <br />
    <br />
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
        <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Periodo</b></td>
        <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Grado</b></td>
        <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Seccion</b></td>
        <td width="25%" align="center" style="background-color: #ECEAE0;"><b>Fecha Inscripcion</b></td>
    </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoAsignacionEstudiantes">   
        <?php
        if ($intNumRows) {

            while ($rTMP = $db->fetch_array($qTMP)) {
                if (count($rTMP) == 0) {
                    $rTMP = array("PERIODO" => null, "GRADO" => null, "SECCION" => null, "FECHA_ASIGNACION" => null,);
                }
                ?>
                <tr id="<?php echo $rTMP["ID"]; ?>">
                    <td width="25%" align="center"><?php echo $rTMP["PERIODO"]; ?></td>
                    <td width="25%" align="center"><?php echo $rTMP["GRADO"]; ?></td>
                    <td width="25%" align="center"><?php echo $rTMP["SECCION"]; ?></td>
                    <td width="25%" align="center"><?php echo $rTMP["FECHA_ASIGNACION"]; ?></td>
                </tr>        
                <?php
            }
        }
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="text/javascript">
        //these two line adds the color to each different row
        $("#tblAcademicoAsignacionEstudiantes tr:even").addClass("eventr");;
        $("#tblAcademicoAsignacionEstudiantes tr:odd").addClass("oddtr");;
        //handle the mouseover , mouseout and click event
        $("#tblAcademicoAsignacionEstudiantes tr").mouseover(function() {
            $(this).addClass("trover");
        }).mouseout(function() {
            $(this).removeClass("trover");
        }).click(function() {
            //$("tr[id!='"+$(this).attr("id")+"']").removeClass("trover");
            //$(this).addClass("trover");        
            //$(this).toggleClass("trclick");
            $("input[name='hdnAcademicoAsignacionEstudiantes_Id']").val( $(this).attr("id") );
            fntAcademicoAsignacionEstudiantesContenido();
        });
    </script>
    <?php
}

function fntAcademicoAsignacionEstudiantesGrabar($db) {
    $strId = isset($_POST["hdnAcademicoAsignacionEstudiantes_Id"]) ? $_POST["hdnAcademicoAsignacionEstudiantes_Id"] : "";
    $strFields = "";
    $strValues = "";

    while ($arrPost = each($_POST)) {
        $arrExplode = explode("_", $arrPost["key"]);
        if ($arrExplode[0] == "txtAcademicoAsignacionEstudiantes") {
            if ($arrPost["key"] != "hdnAcademicoAsignacionEstudiantes_Id") {
                if ($arrExplode[1] == "FECHA-ASIGNACION" || $arrExplode[1] == "FECHA-EXPIRACION") {
                    $arrTMP = array();
                    $arrTMP = explode("-", $arrPost["value"]);
                    krsort($arrTMP);
                    $arrPost["value"] = implode("-", $arrTMP);
                }
                if ($arrExplode[1] == "FK-ESTUDIANTE") {
                    $strEstudiante = $arrPost["value"];
                }
                if ($arrExplode[1] == "fk-periodo") {
                    $intPeriodo = $arrPost["value"];
                }
                if (empty($strId)) {
                    $strFields .= (empty($strFields)) ? "" : ",";
                    $strFields .= strtoupper(str_ireplace("-", "_", $arrExplode[1]));

                    $strValues .= empty($strValues) ? "" : ",";
                    $strValues .= "'{$arrPost["value"]}'";
                } else {
                    $strFields .= (empty($strFields)) ? "" : ",";
                    $strFields .= strtoupper(str_ireplace("-", "_", $arrExplode[1])) . " = '{$arrPost["value"]}'";
                }
            }
        }
    }
    if (empty($strId)) {
        $strQuery = "SELECT * FROM cllg_academico_estudiantes_asignacion WHERE FK_ESTUDIANTE = '{$strEstudiante}' AND FK_PERIODO = '{$intPeriodo}'";
        $qTMP = $db->queryToArray($strQuery);
        $intNumRows = count($qTMP);
        if ($intNumRows == 0) {
            $db->query("INSERT INTO cllg_academico_estudiantes_asignacion ({$strFields}) VALUES ({$strValues})");
        } else {
            
        }
    } else {
        $strQuery = "SELECT * FROM cllg_academico_estudiantes_asignacion WHERE FK_ESTUDIANTE = '{$strEstudiante}' AND FK_PERIODO = '{$intPeriodo}'";
        $qTMP = $db->query($strQuery);
        $intNumRows = $db->num_rows;
        $db->free_result($qTMP);
        if ($intNumRows == 0) {
            $db->query("UPDATE cllg_academico_estudiantes_asignacion SET {$strFields} WHERE ID = '{$strId}'");
        }
    }
}
?>